﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using OgilvyOne.Model;
using OgilvyOne.Utility;

namespace OgilvyOne.BLL
{
    /// <summary>
    /// Cms解析基类
    /// </summary>
    abstract class CommonModel : ConConstants
    {
        protected CmsVo vo;
        public CmsVo CmsVo
        {
            get { return vo; }
        }
        //内容
        protected CmsArticleModel article;
        //栏目相关
        protected MenuModel menupage;
        public MenuModel MenuModel
        {
            get { return menupage; }
        }
        protected string html ; //模板HTML
        public CommonModel(CmsVo vo)
        {
            this.vo = vo;
            this.article = vo.CmsArticleInfo;
            
            this.html = vo.TempInfo.TemCotent;
            if (vo.Cid > 0)
            {//存在栏目ID
                menupage = new MenuModel(vo.Cid);
            }
            else
            {
                menupage = new MenuModel();
            }
            
        }
        /// <summary>
        /// Cms 模板标签正则定义 形如: <cms:content value="GetTest"></cms:content>
        /// </summary>
        static Regex regex = new Regex("<cms:\\w+\\s+value=[\'|\"][\\w\\.\\s\\*]+[\'|\"][\\s\\w\'\"\\=]*>[\\[\\]\\&\\;\\!\'\"\\=<>/\\w\\s]*</cms:\\w+>");

        public string GetHtml()
        {
            
            
            MatchCollection mc = regex.Matches(html);

            foreach (Match m in mc)
            {
                string mv = m.Value;
                html = StringUtil.ReplaceStr(html, mv, LabelProxy.GetCmsLabel(this, mv).GetLabelHtml());
            }

            return html;
        }
        //测试
        public string GetTest()
        {
            return ContentHtmlPage.GetTest();
        }
        /// <summary>
        /// 当前内容信息
        /// </summary>
        /// <returns></returns>
        public CmsArticleModel GetConInfo()
        {
            return vo.CmsArticleInfo;
        }
        /// <summary>
        /// 当前栏目信息
        /// </summary>
        /// <returns></returns>
        public ColumnModel GetColumnInfo()
        {
            return menupage.GetColumn();
        }

        /// <summary>
        /// 当前内容标题
        /// </summary>
        /// <returns></returns>
        public string GetTitle()
        {
            if (vo.CmsArticleInfo != null)
            {
                return vo.CmsArticleInfo.Title;
            }
            return "";
        }
        /// <summary>
        /// 当前内容详细内容
        /// </summary>
        /// <returns></returns>
        public string GetContent()
        {
            if (vo.CmsArticleInfo != null)
            {
                return vo.CmsArticleInfo.Content;
            }
            return "";
        }
        public string GetTitleImg()
        {
            if (vo.CmsArticleInfo != null)
            {
                return "/uploadfiles/image/" + vo.CmsArticleInfo.Titleimg;
            }
            return "";
        }
    }
}
